
$.cookie={};
$.cookie.set=function(name,value){//两个参数，一个是cookie的名子，一个是值
    //var Days = 30; //此 cookie 将被保存 30 天
    //var exp  = new Date();    //new Date("December 31, 9998");
    //exp.setTime(exp.getTime() + Days*24*60*60*1000);
    //document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
	document.cookie = name + "="+ escape (value) + ";";
}
$.cookie.get = function(name){//取cookies函数
    var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
     if(arr != null) return unescape(arr[2]); return null;
}
 $.cookie.del = function(name){//删除cookie
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval=getCookie(name);
    if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();
}

$.request = function(cmd, form, url, callback){
	var jsonObj = $("#"+form).serializeObject(cmd);
	$.postJson(jsonObj,url, callback);
}
$.postJson = function(jsonObj,url, callback){
	//var jsonuserinfo = $.toJSON($('#frmVerifyCode').serializeObject(rpc.RegRequest));
	var json = $.toJSON(jsonObj);
	jQuery.ajax({
		type : 'POST',
		contentType : 'application/json',
		url : url,
		data : json,
		dataType : 'json',
		success : function(data) {
			if (data.code ==0 || !data.code) {
				callback(data.result);
			}else{
				alert(data.msg);
			}
		},
		error : function(data) {
			alert("error")
		}
	});
}

$.fn.serializeObject = function(cmd) {
	var result = {
		cmd : cmd
	};
	var o = {};
	result.request = o;
	var a = this.serializeArray();
	$.each(a, function() {
		if(this.value=="") return;
		if (o[this.name]) {
			if (!o[this.name].push) {
				o[this.name] = [ o[this.name] ];
			}
			o[this.name].push(this.value || '');
		} else {
			o[this.name] = this.value || '';
		}
	});
	return result;
};

/*执行跨域的js*/
$.loadJS =function oadJS(urlStr, scriptName, completeEvt, obj) {
	if (document.getElementsByTagName("head").length < 0) return;
	var head = document.getElementsByTagName("head").item(0);
	if (!head) return;
	if (scriptName) {
		var old = document.getElementById(scriptName);
		if (old) head.removeChild(old);
	}
	var script = document.createElement("SCRIPT");
	script.src = urlStr;
	script.defer = true;
	script.type = "text/javascript";
	if (scriptName) { script.id = scriptName; }
	if (completeEvt) {
		script.onload = function() { completeEvt(obj); }
		script.onreadystatechange = function() {
			if (script.readyState == 'complete' || script.readyState == 'loaded') { completeEvt(obj); }
		}
	}
	head.appendChild(script);
}
/*执行跨域的js*/
jQuery.fn.loadJS =function (opt, completeEvt) {
	if (document.getElementsByTagName("head").length < 0) return;
	var head = document.getElementsByTagName("head").item(0);
	if (!head) return;
	if (opt.id) {
		var old = document.getElementById("#"+opt.id);
		if (old) head.removeChild(old);
	}
	var script = document.createElement("SCRIPT");
	script.src = opt.url;
	//script.defer = true;
	script.type = "text/javascript";
	if(opt!=null){
		for(var k in opt){
			if(k == "url" || k=="id") continue;
			script.setAttribute(k,opt[k]);
		}
	}
	if (opt.id) { script.id = opt.id; }
	if (completeEvt) {
		script.onload = function() { completeEvt(opt.obj); }
		script.onreadystatechange = function() {
			if (script.readyState == 'complete' || script.readyState == 'loaded') { completeEvt(opt.obj); }
		}
	}
	head.appendChild(script);
}
$.fn.imgResize = function(parentDem,imgDem){
    if(imgDem.w>0 && imgDem.h>0){
        var rate = (parentDem.w/imgDem.w < parentDem.h/imgDem.h)?parentDem.w/imgDem.w:parentDem.h/imgDem.h;
        //如果 指定高度/图片高度  小于  指定宽度/图片宽度 ，  那么，我们的比例数 取 指定高度/图片高度。
        //如果 指定高度/图片高度  大于  指定宽度/图片宽度 ，  那么，我们的比例数 取 指定宽度/图片宽度。
        if(rate <= 1){   
            imgDem.w = imgDem.w*rate; //图片新的宽度 = 宽度 * 比例数
            imgDem.h = imgDem.h*rate;
        }else{//  如果比例数大于1，则新的宽度等于以前的宽度。
            imgDem.w = imgDem.w;
            imgDem.h = imgDem.h;
        }
    }
    return imgDem;
}
$.fn.imgauto = function(options){
	var defaults = {
		width: "auto",
		height: "auto",
		middle: true
	};
	var options = $.extend(defaults, options);
	return this.each(function(){
		var image = $(this);
		var width = options.width == "auto" ? image.parent().width() : options.width;
		var height = options.height == "auto" ? image.parent().height() : options.height;
		var dem = $(this).imgResize({w:width,h:height},{w:image.width(),h:image.height()});
		
		image.width(dem.w);
		image.height(dem.h);
		image.css("margin-left",(width-image.width())/2+"px");
		image.css("margin-top",(height-image.height())/2+"px");
	});
};
Date.prototype.format=function(fmt) {         
     var o = {         
     "M+" : this.getMonth()+1, //月份         
     "d+" : this.getDate(), //日         
     "h+" : this.getHours()%12 == 0 ? 12 : this.getHours()%12, //小时         
     "H+" : this.getHours(), //小时         
     "m+" : this.getMinutes(), //分         
     "s+" : this.getSeconds(), //秒         
     "q+" : Math.floor((this.getMonth()+3)/3), //季度         
     "S" : this.getMilliseconds() //毫秒         
     };         
     var week = {         
     "0" : "\u65e5",         
     "1" : "\u4e00",         
     "2" : "\u4e8c",         
     "3" : "\u4e09",         
     "4" : "\u56db",         
     "5" : "\u4e94",         
     "6" : "\u516d"        
     };         
     if(/(y+)/.test(fmt)){         
         fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));         
     }         
     if(/(E+)/.test(fmt)){         
         fmt=fmt.replace(RegExp.$1, ((RegExp.$1.length>1) ? (RegExp.$1.length>2 ? "\u661f\u671f" : "\u5468") : "")+week[this.getDay()+""]);         
     }         
     for(var k in o){         
         if(new RegExp("("+ k +")").test(fmt)){         
             fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));         
         }         
     }         
     return fmt;         
}  
String.prototype.replaceAll = function(search, replace){  
 var regex = new RegExp(search, "g");  
 return this.replace(regex, replace);  
}
function parseTime(longTime){
	if(!longTime) return;
	var d=	new Date();
	d.setTime(longTime);
	return d.format('yyyy-MM-dd HH:mm:ss');
} 
function parseData(longTime){
	if(!longTime) return;
	var d=	new Date();
	d.setTime(longTime);
	return d.format('yyyy-MM-dd');
}